import { Router } from 'express'
import { loadUserData, saveUserData } from '../utils/fileStore.js'

const router = Router()

router.get('/:userId', (req, res) => {
  const { userId } = req.params
  const userData = loadUserData(userId)
  res.json({ code: 200, data: userData, msg: '获取进度成功' })
})

router.post('/:userId', (req, res) => {
  const { userId } = req.params
  const progressData = req.body
  const userData = loadUserData(userId)
  userData.subjects = { ...userData.subjects, ...progressData.subjects }
  userData.settings = { ...userData.settings, ...progressData.settings }
  if (saveUserData(userId, userData))
    return res.json({ code: 200, data: null, msg: '进度保存成功' })
  return res.status(500).json({ code: 500, data: null, msg: '保存失败' })
})

export default router
